package com.qdxwx.data.service.order;

import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.mapper.order.OrderAllotDetailMapper;
import com.qdxwx.models.company.Company;
import com.qdxwx.models.order.Order;
import com.qdxwx.models.order.OrderAllotDetail;
import com.qdxwx.models.order.OrderDetail;
import com.qdxwx.models.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Service
public class OrderAllotDetailServiceImpl implements OrderAllotDetailService {

    @Autowired
    private OrderAllotDetailMapper orderAllotDetailMapper;

    @Override
    public int insert(OrderAllotDetail allotDetail) {
        allotDetail.setCreateTime(LocalDateTime.now());
        return orderAllotDetailMapper.insertOrderAllotDetail(allotDetail);
    }

    @Override
    public OrderAllotDetail get(int id) {
        if (id < 1) return null;
        return orderAllotDetailMapper.selectById(id);
    }

    @Override
    public List<OrderAllotDetail> getList(OrderAllotDetail allotDetail, PageRequest page) {
        return orderAllotDetailMapper.selectList(allotDetail, page);
    }

    @Override
    public List<OrderAllotDetail> getListByOrderNo(String orderNo) {
        if(TKString.isEmpty(TKString.trim(orderNo))) return new ArrayList<>();
        OrderAllotDetail allotDetail = new OrderAllotDetail();
        allotDetail.setOrderNo(orderNo);
        return orderAllotDetailMapper.selectList(allotDetail, null);
    }

    @Override
    public int count(OrderAllotDetail allotDetail) {
        return orderAllotDetailMapper.count(allotDetail);
    }

    @Override
    public void create(Order order, User user, int rate, int amount, String desc) {
        if (order == null || user == null) return;
        create(order.getOrderId(), order.getOrderNo(),order.getPayAmount()
                , 0, user.getUserId(), user.getDisplayName()
                , rate, amount, desc);
    }

    @Override
    public void create(Order order, Company company, int rate, int amount, String desc) {
        if (order == null || company == null) return;
        create(order.getOrderId(), order.getOrderNo(),order.getPayAmount()
                , 1, company.getCompanyId(), company.getCompanyName()
                , rate, amount, desc);
    }

    @Override
    public void create(OrderDetail orderDetail, User user, int rate, int amount, String desc) {
        if (orderDetail == null || user == null) return;
        create(orderDetail.getOrderId(), orderDetail.getOrderNo(), orderDetail.getPrice()
                , 0, user.getUserId(), user.getDisplayName()
                , rate, amount, desc);
    }

    @Override
    public void create(OrderDetail orderDetail, Company company, int rate, int amount, String desc) {
        if (orderDetail == null || company == null) return;
        create(orderDetail.getOrderId(), orderDetail.getOrderNo(), orderDetail.getPrice()
                , 1, company.getCompanyId(), company.getCompanyName()
                , rate, amount, desc);
    }

    @Override
    public void create(int orderId, String orderNo, int payAmount, int accountType, long accountId, String accountName, int rate, int amount, String desc) {
        OrderAllotDetail detail = new OrderAllotDetail();
        detail.setOrderId(orderId);
        detail.setOrderNo(orderNo);
        detail.setPayAmount(payAmount);
        detail.setAllotRate(rate);
        detail.setAllotAmount(amount);
        detail.setAccountType(accountType);
        detail.setAccountId(accountId);
        detail.setAccountName(accountName);
        detail.setRemark(desc);
        insert(detail);
    }

    @Override
    public int updateCombine(long userId1, long userId2) {
        if(userId1 < 1 || userId2 < 1){
            throw new RuntimeException("更新返现订单：参数错误");
        }
        return orderAllotDetailMapper.updateCombine(userId1, userId2);
    }
}
